package co2.bean;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;

import org.hibernate.id.UUIDHexGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

import co2.engine.BeanGetter;
import co2.engine.DAOInterface;
import co2.pojos.PaketSoal;

@ManagedBean
@ViewScoped
public class PaketSoalBean extends AbstractBean implements Serializable {
	private static final long serialVersionUID = 1L;

	private PaketSoal paketSoal;
	private PaketSoal selectedPS;
	private List<PaketSoal> list;
	
	@Autowired
	@Qualifier("daoI")
	private DAOInterface dao;
	
	@PostConstruct
	public void postCon(){
		list = dao.getAllPaketSoal();
		paketSoal = new PaketSoal();
		selectedPS = new PaketSoal();
	}
	public void paSave(){
		String id = (String) (new UUIDHexGenerator()).generate(null, null);
		paketSoal.setId(id);
		try{
			dao.save(paketSoal);
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO,"Success",""));
			paketSoal = new PaketSoal();
			list = dao.getAllPaketSoal();
		}catch(Exception e){
			e.printStackTrace();
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,"Error Saving Exception",""));
		}
	}
	public void update(){
		try{
			dao.update(paketSoal);
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Update Success",""));
			paketSoal = new PaketSoal();
			list = dao.getAllPaketSoal();
		}catch(Exception e){
			e.printStackTrace();
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,"Error Update Exception", ""));
		}
	}
	public void redirectBank(){
		try{
			Map map = FacesContext.getCurrentInstance().getExternalContext().getSessionMap();
			SignInBean signInBean = (SignInBean) map.get("signInBean");
			signInBean.getMap().put("paketSoal", selectedPS);
			
			String requestPath = FacesContext.getCurrentInstance().getExternalContext().getRequestContextPath() ;
			FacesContext.getCurrentInstance().getExternalContext().redirect(requestPath+"/jsf/target/list_bank.jsf");
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	public void delete(){
		try{
			dao.delete(PaketSoal.class, paketSoal.getId());
			list = dao.getAllPaketSoal();
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO,"Deleted",""));
		}catch(Exception e){
			e.printStackTrace();
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,"Error Delete",""));
		}
	}
	public PaketSoal getPaketSoal() {
		return paketSoal;
	}
	public void setPaketSoal(PaketSoal paketSoal) {
		if(paketSoal==null){
			this.paketSoal = new PaketSoal();
		}else{
			this.paketSoal = paketSoal;
		}
	}
	public PaketSoal getSelectedPS() {
		return selectedPS;
	}
	public void setSelectedPS(PaketSoal selectedPS) {
		this.selectedPS = selectedPS;
	}
	public List<PaketSoal> getList() {
		return list;
	}
	public void setList(List<PaketSoal> list) {
		this.list = list;
	}
	public DAOInterface getDao() {
		return dao;
	}
	public void setDao(DAOInterface dao) {
		this.dao = dao;
	}
}
